package com.mostromas.ess.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mostromas.ess.entities.ProjectCardNumber;
import com.mostromas.ess.project.mapper.ProjectCardNumberMapper;
import com.mostromas.ess.project.req.ProjectCardNumberREQ;
import com.mostromas.ess.project.service.IProjectCardNumberService;
import com.mostromas.ess.util.base.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProjectCardNumberServiceImpl extends ServiceImpl<ProjectCardNumberMapper, ProjectCardNumber> implements IProjectCardNumberService {

    @Override
    public Result queryList(ProjectCardNumberREQ req) {
        QueryWrapper<ProjectCardNumber> wrapper = new QueryWrapper<>();
        if(req.getType() != null){
            wrapper.eq("type",req.getType());
        }
        if(req.getState() != null){
            wrapper.eq("state",req.getState());
        }
        if(req.getUsername() != null){
            wrapper.eq("username",req.getUsername());
        }
        if(req.getProjectId() != null){
            wrapper.eq("project_id",req.getProjectId());
        }
        wrapper.orderByAsc("project_id");
        wrapper.orderByAsc("type");
        wrapper.orderByAsc("state");
        return Result.ok(baseMapper.selectPage(req.getPage(),wrapper));
    }

    @Transactional
    public Result saveList(List<ProjectCardNumber> list) {
        boolean state = this.saveBatch(list);
        return state?Result.ok():Result.error("插入错误");
    }

    @Override
    public ProjectCardNumber checkCardNumberByProjectId(ProjectCardNumberREQ req) {
        QueryWrapper<ProjectCardNumber> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id",req.getProjectId());
        wrapper.eq("card_number",req.getCardNumber());
        wrapper.eq("state",false);
        return baseMapper.selectOne(wrapper);
    }
}
